﻿//-----------------------------------------------------------------------
// <copyright>
//     Copyright (c) Artur Mustafin. All rights reserved.
// </copyright>
//-----------------------------------------------------------------------

using System.Collections.Generic;
using System.ComponentModel.Composition;
using System.ComponentModel.Composition.Hosting;
using System.Reflection;
using Math.Compiler.Interfaces;

namespace Math.Compiler.Classes
{
    internal class Externals
    {
        private static Externals _instance = new Externals();

        private Externals()
        {
        }

        static Externals()
        {
            _instance = new Externals();
        }

        public static void ImportExternals()
        {
            using (var builtIns = new AssemblyCatalog(typeof(Externals).Assembly))
            using (var extensions = new DirectoryCatalog("AddIns"))
            using (var catalog = new AggregateCatalog(builtIns, extensions))
            using (var container = new CompositionContainer(catalog))
            {
                container.ComposeParts(_instance);
            }
        }

        [ImportMany(AllowRecomposition = true)]
        private IEnumerable<IFunction> InternalFunctions { get; set; }
        [ImportMany(AllowRecomposition = true)]
        private IEnumerable<IEnumerableFunction> InternalEnumerableFunctions { get; set; }

        public static IEnumerable<IFunction> Functions { get { return _instance.InternalFunctions; } }
        public static IEnumerable<IEnumerableFunction> EnumerableFunctions { get { return _instance.InternalEnumerableFunctions; } }
    }
}
